<template>
	<view class="user-index">
				
		<view class="user-info">
			<view class="avatar">
				<image :src="userinfo.head_ico"></image>
			</view>
			
			<view class="info"  @tap="checkLogin()">
				<block v-if="userinfo.username">
					<view class="user-name">{{userinfo.username}}</view>
					<view class="point text-gray">余额: {{userinfo.balance}}</view>
					<view class="point text-gray">积分: {{userinfo.integral}}</view>
				</block>
				<view class="tips" v-else>请登录</view>
			</view>
			
			<view class="iconfont icon-you"></view>
		</view>
		

		
		<view class="notice" v-if="notices.length > 0">
			<view class="notice-icon">
				<view class="iconfont icon-tixing"></view>
			</view>
			<swiper :vertical="true" :autoplay="true" :circular="true" :interval="3000">
				<swiper-item v-for="(item, index) in notices" :key="index" @tap="toAtricleDetail(item)">
					<navigator class="uni-ellipsis">{{item.title}}</navigator>
				</swiper-item>
			</swiper>
		</view>
		
		<view class="top-menu">
			<view class="item"  v-for="(item,index) in topMenu" :key="index" @tap="toTarget(item)">
				<view class="icon">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="name">{{item.name}}</view>
			</view>
		</view>

		
		<view class="top-menu">
			<view class="item"  v-for="(item,index) in menuList" :key="index" @tap="toTarget(item)">
				<view class="icon">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="name">{{item.name}}</view>
			</view>
		</view>

	</view>
</template>

<script>
	import uniNavBar from '../../components/uni-nav-bar.vue';
	import Request from '../../common/request.js';
	
	
	export default {
		components: {
			uniNavBar
		},
		data() {
			return {
				userinfo: {
					head_ico: '/static/logo.png'
				},
				token: '',
				notices: [],
				detail: {},
				topMenu: [
					{
						icon: '/static/icon/me/me_ic_add.png',
						name: '地址管理',
						url: '/pages/address/index',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_Aut.png',
						name: '实名认证',
						url: '/pages/my/verified',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_order.png',
						name: '我的订单',
						url: '/pages/order/index',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_aftersale.png',
						name: '退换货',
						url: '/pages/aftersales/index',
						type: ''
					}
				],
				menuList: [
					{
						icon: '/static/icon/me/me_ic_code.png',
						name: '发布/求购',
						url: '/pages/demand/my',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_code.png',
						name: '我要分享',
						url: '/pages/my/qrcode',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_Col.png',
						name: '我的收藏',
						url: '/pages/favorite/index',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_Cash.png',
						name: '申请提现',
						url: '/pages/funds/withdraw-record',
						type: ''
					},
					
					{
						icon: '/static/icon/me/me_ic_Acc.png',
						name: '收款帐号',
						url: '/pages/funds/account',
						type: ''
					},

					{
						icon: '/static/icon/me/me_ic_Opinion.png',
						name: '意见反馈',
						url: '/pages/feedback/index',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_guide.png',
						name: '新手指引',
						url: 'help',
						type: ''
					},
// 					{
// 						icon: '/static/icon/me/me_ic_Fans.png',
// 						name: '我的粉丝',
// 						url: '/pages/my/sons',
// 						type: ''
// 					},
					{
						icon: '/static/icon/me/me_ic_about.png',
						name: '关于我们',
						url: 'about',
						type: ''
					},
					{
						icon: '/static/icon/me/webmail.png',
						name: '消息中心',
						url: '/pages/feedback/webmail',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_Rechargex.png',
						name: '充值中心',
						url: '/pages/recharge/recharge',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_Ordercenter.png',
						name: '资金明细',
						url: '/pages/funds/detail',
						type: ''
					},
					{
						icon: '/static/icon/me/me_ic_Ordercenter.png',
						name: '联系客服',
						url: '/pages/funds/detail',
						type: ''
					}
				]
			};
		},
		onLoad() {
			this.getNotice();
		},
		onShow(){
			
			this.token = uni.getStorageSync('token');
			this.userinfo = {};
			
			this.getUserInfo();
		},
		onPullDownRefresh() {
			this.getUserInfo(() => {
				uni.stopPullDownRefresh()
			})
		},
		methods:{
			//获取用户信息
			getUserInfo(callback){
				if(!this.token){
					this.userinfo = {};
					return false;
				}
				var that = this;				
				var params = {
					url: 'user/index',
					sCallback: function(res) {
						that.detail = res.data;
						that.userinfo = res.data.userInfo;
						if(!res.data.userInfo.head_ico) {
							that.userinfo.head_ico = '/static/logo.png';
						}
						callback && callback();
					}
				}
				
				Request.send(params);
			},
			//获取公告
			getNotice () {
				var that = this;
				var params = {
					url: 'index/notice',
					sCallback: function(res) {
						that.notices = res.data;
					}
				}
				
				Request.send(params);
			},
			//去登录
			checkLogin(){
				if(!this.token) {
					uni.navigateTo({
						url: '/pages/my/login'
					})
				} else {
					uni.navigateTo({
						url: '/pages/setting/index'
					})
				}
				
			},
			//去站内信
			toWebmail(){
				uni.navigateTo({
					url: '/pages/feedback/webmail'
				})
			},
			//去公告详情
			toAtricleDetail(item) {
				uni.navigateTo({
					url: '/pages/article/detail?id='+item.id+'&title='+item.title
				})
			},
			toTarget(item) {
				if(!this.userinfo){
					return ;
				}
				console.log(item);
				
				switch(item.url) {
					case '/pages/my/qrcode':
						if(this.userinfo.level == 0) {
							Request.alert('VIP及代理才有推广权限');
							return;
						};
						break;
					case '/pages/usershop/menu':
						if(this.detail.shop_status == 0) {
							Request.alert(this.detail.shop_tips);
							return;
						};
						break;
					case 'about':
						if(this.detail.about.id) {
							uni.navigateTo({
								url: '/pages/article/detail?id='+this.detail.about.id
							})
						};
						return;
						break;
					case 'help':
						if(this.detail.help.id) {
							uni.navigateTo({
								url: '/pages/article/detail?id='+this.detail.help.id
							})
						};
						return;
						break;
					default:;
					
				}
				if(item.type == 'tab') {
					uni.switchTab({
						url: item.url
					})
				} else {
					uni.navigateTo({
						url: item.url
					})
				}
			},
			checkLevel(){
				console.log('aaa');
			},
			//我的钱包
			toWallet: function(){
				uni.navigateTo({
					url: '/pages/funds/index?phone_time='+this.userinfo.phone_time
				})
			}
		}
	}
</script>

<style>
	.user-info{
		background: #fff;
		border-top: 1upx solid #eee;
		display: flex;
		align-items: center;
		padding: 20upx;
		font-size: 28upx;
		margin-top: 20upx;
	}
	
	.user-info image{
		width: 160upx;
		height: 160upx;
	}
	
	.user-info .info{
		padding-left: 20upx;
		line-height: 50upx;
		flex: 1;
	}

	.user-info .info .iconfont{
		width: 80upx;
		color: #666;
	}
	
	.notice{
		display: none;
		background: #fff;	
		color: #666;
		height: 70upx;
		line-height: 70upx;
		margin-top: 20upx;
	}
	
	.notice .notice-icon{
		width: 60upx;
		text-align: center;
	}
	
	.notice swiper{
		flex: 1;
		font-size: 24upx;
		height: 70upx;
	}
	
	.top-menu{
		background: #fff;
		display: flex;
		margin-top: 20upx;
		padding-top: 30upx;
		width: 750upx;
		flex-wrap: wrap;
		align-items: center;
	}

	.top-menu.amount{
		align-items: baseline;
	}

	.top-menu .text .icon{
		line-height: 60upx;
		font-weight: 600;
		font-size: 28upx;
	}
	
	.top-menu .item{
		width: 187.5upx;
		text-align: center;
		font-size: 24upx;
		color: #333;
		margin-bottom: 30upx;
	}
	
	.funds .item{
		width: 150upx;
	}
	
	.count .item{
		width: 250upx;
	}

	.top-menu .wallet{
		border-left: 1upx solid #eee;
		box-sizing: border-box;
	}

	.top-menu .item .icon{
		/* margin-bottom: 16upx; */
	}
	
	.top-menu .item .icon image{
		width: 60upx;
		height: 60upx;
	}
	
	.menu{
		margin-top: 10upx;
		font-size: 24upx;
	}
	
	.menu .uni-list-cell-navigate{
		justify-content: flex-start;
	}
	
	.menu .uni-list-cell-navigate .iconfont{
		font-size: 40upx;
		margin-right: 10px;
	}
</style>
